package com.xasxt.book.app.utils;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

@Configuration
public class ShiroConfiguration {

	/**
	 * 配置shrio的生命周期归spring管理
	 * @return
	 */
	@Bean(name="getLifecycleBeanPostProcessor")
	public  LifecycleBeanPostProcessor   getLifecycleBeanPostProcessor() {
		return new LifecycleBeanPostProcessor();
	}

	/**
	 * 配置用户定义的域
	 * @return
	 */
	@Bean
	@DependsOn("getLifecycleBeanPostProcessor")
	public   MyRealm  getMyRealm() {
		return new MyRealm();
	}

	/**
	 * 配置shiro的安全管理器归spring管理
	 * @return
	 */
	@Bean(name="getDefaultWebSecurityManager")
	public DefaultWebSecurityManager  getDefaultWebSecurityManager () {
		DefaultWebSecurityManager  defaultWebSecurityManager=new DefaultWebSecurityManager();
		defaultWebSecurityManager.setRealm(getMyRealm());
		return defaultWebSecurityManager;
	}
	/**
	 * 配置过滤规则
	 * @return
	 */
	@Bean(name="shiroFilter")
	public   ShiroFilterFactoryBean   getShiroFilterFactoryBean() {
		ShiroFilterFactoryBean    shiroFilterFactoryBean=new ShiroFilterFactoryBean();
		shiroFilterFactoryBean.setSecurityManager(getDefaultWebSecurityManager ());
		Map<String,String>map=new LinkedHashMap<String, String>();
		map.put("/login", "anon");
		map.put("/jquery/**", "anon");
//		map.put("/logout", "logout");
		map.put("/**", "authc");
		
		shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
		return shiroFilterFactoryBean;
	}







}
